package com.kitty.game.pet.handler;

import com.kitty.game.equip.service.EquipService;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.enter.RespUpdate;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.pet.service.PetService;
import com.kitty.game.utils.Const;
import com.kitty.mina.message.MessagePusher;
import com.kitty.game.pet.service.BookService;
import com.kitty.game.fight.service.BroadcastService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component("UsePetItemHandler")
public class UsePetItemHandler extends FeedPetHandler {

    @Autowired
    BroadcastService broadcastService;
    @Override
    public String acceptFeed(Pet pet, Role role, RoleEquip roleEquip) {
        String itemName =roleEquip.getName();
        String bookNames = "寒冰惊雷狂暴烈炎魔引怒击破天青木碎石降魔斩修罗术反击云体仙风尽忠寒冰·金色惊雷·金色狂暴·金色烈炎·金色魔引·金色怒击·金色破天·金色青木·金色碎石·金色降魔斩·金色修罗术·金色反击·金色云体·金色仙风·金色尽忠·金色";
        if (itemName.equals("风灵丸")) {
            int currTime = new Long(new Date().getTime() / 1000).intValue();
            BroadcastService broadcastService = SpringUtils.getBean(BroadcastService.class);
            boolean isLimit = roleEquip.isLimit();

            if (roleEquip.queryNum() <= 0) {
                return MessageFormat.format(Const.ROLE_RESOURCE_NOT_ENOUGH, roleEquip.getName());
            }
            roleService.addAchieveScore(role,"201026");
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("你消耗了一颗风灵丸为#R" + pet.getName() + "#n补充了5天风灵丸时间。"));
            equipService.delEquipNum(role,roleEquip, 1);
            if (isLimit) {
                petService.addLimitTime(pet, 1);
            }
            int mountTime = pet.getMountTime();
            if (mountTime < currTime) {
                mountTime = currTime;
            }
            pet.setMountTime(mountTime + 86400 * 5);
            role.save();
            int fenglingwanNum = (pet.getMountTime() - currTime) / 85000 / 5;
            int moveSpeed = fenglingwanNum * 5;
            if (moveSpeed > pet.getRank() * 5) {
                moveSpeed = pet.getRank() * 5;
            }

            RespUpdate respUpdate = new RespUpdate();
            List<FiedValue> list = new ArrayList<>();
            list.add(new FiedValue(883, pet.getMountTime()));
            list.add(new FiedValue(270, pet.getLimitTime()));
            list.add(new FiedValue(884, moveSpeed));
            respUpdate.setRoleId(pet.getId());
            respUpdate.setList(list);
            MessagePusher.pushMessage(role, respUpdate);
            if (fenglingwanNum <= pet.getRank()) {
                int posture = petService.getMountPosture(role, pet);
                role.setMoveSpeed(moveSpeed);
                role.setMount(pet.getId());
                role.setMountIcon(pet.getRankItem());
                role.setMountPosture(posture);
                broadcastService.sendUpdateAppear(role);
                SpringUtils.getRoleService().sendExtraAttribute(role);
                role.save();
            }
        } else if (bookNames.contains(itemName)) {//天书
            SpringUtils.getBean(BookService.class).useBook(role, pet, roleEquip);
        } else if (itemName.contains("秘笈")) {//使用天生技能
            SpringUtils.getBean(PetService.class).useSkillBook(role, pet, roleEquip);
        }else if (roleEquip.getName().equals("彩凤之魂")&&pet.getRank()>=5){
            pet.setZhuru((byte) 1);
            role.save();
            petService.loadPet(role,pet);
            broadcastService.sendAppear(role);
            SpringUtils.getBean(EquipService.class).delEquipNum(role,roleEquip,1);
        }
        petService.refreshPetInfo(role, pet);
        return null;
    }
}
